package com.scy.weibo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        //文件路径
        String driPath = "d:/files";
        //日期路径
        String dataPath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
        File dirFile = new File(driPath + dataPath);
        //判断文件夹存不存在 不存在则创建
        if (dirFile.exists()){
            dirFile.mkdir();
        }
        //保存文件到指定我呢见驾 异常抛出
        picFile.transferTo(new File(driPath+dataPath+fileName));
        return dataPath+fileName;
    }
    @RequestMapping("/remove")
    public void remove(String url){
        new File("d:/files"+url).delete();
    }
}
